package com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.bean.BCustomerFeightAgreementDetailPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.customer_manage.freight_agreement.detail.mapper
 * @Author: luoge
 * @CreateTime: 2021-01-21 18:05
 * @Description:
 */

@Mapper
@Component
public interface BCustomerFreightAgreementDetailMapper {

    class BCustomerFreightAgreementDetailMapperProvider{
        public String findAll(Map map){

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_freight_agreement_detail t ");
                WHERE("1=1");
            }}.toString();

            if(map!=null){

                String agreementFreightId = DataTypeUtil.toStringObject(map.get("agreementFreightId"));
                if(agreementFreightId != null){
                    sql += " and t.agreement_freight_id = " + DataTypeUtil.quoteStr(agreementFreightId);
                }
            }
            /**
             * standard_type_id
             * goods_type_id
             */
            sql += " order by t.agreement_freight_id, t.standard_type_id asc,t.goods_type_id asc ,t.start_value asc";
            return sql;
        }

        public String deleteByAgreementFreightId(String freightAgreementId){
            String sql = new SQL() {{
                DELETE_FROM("b_customer_freight_agreement_detail t");
                WHERE("t.agreement_freight_id = #{freightAgreementId}");
            }}.toString();
            return sql;
        }

        public String batchDeleteByAgreementFreightIds(String agreementFreightIds){
            String sql = new SQL() {{
                DELETE_FROM("b_customer_freight_agreement_detail t");
                WHERE("t.agreement_freight_id in (#{agreementFreightIds})");
            }}.toString();
            return sql;
        }
    }

    @Results(id = "bCustomerFreightAgreementDetailMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "agreement_freight_type_id", property = "agreementFreightTypeId"),
            @Result(column = "agreement_freight_id", property = "agreementFreightId"),
            @Result(column = "standard_type_id", property = "standardTypeId"),
            @Result(column = "goods_type_id", property = "goodsTypeId"),
            @Result(column = "start_value", property = "startValue"),
            @Result(column = "end_value", property = "endValue"),
            @Result(column = "price", property = "price"),
            @Result(column = "comment", property = "comment")
    })
    @SelectProvider(type = BCustomerFreightAgreementDetailMapperProvider.class, method = "findAll")
    List<BCustomerFeightAgreementDetailPojo> findAll(Map param);

    @Insert(
            "<script>" +
                    "insert into b_customer_freight_agreement_detail(id, agreement_freight_type_id, agreement_freight_id, standard_type_id, goods_type_id, start_value, end_value, price, comment) values " +
                    "<foreach collection='detailPojos' item='pojo' separator=','>" +
                    "(#{pojo.id}, #{pojo.agreementFreightTypeId}, #{pojo.agreementFreightId}, #{pojo.standardTypeId}, #{pojo.goodsTypeId}, #{pojo.startValue}, #{pojo.endValue}, #{pojo.price}, #{pojo.comment})" +
                    "</foreach>" +
            "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsert(@Param("detailPojos") List<BCustomerFeightAgreementDetailPojo> detailPojos);

    @DeleteProvider(type = BCustomerFreightAgreementDetailMapperProvider.class, method = "deleteByAgreementFreightId")
    int deleteByAgreementFreightId(String agreementFreightId);

    @DeleteProvider(type = BCustomerFreightAgreementDetailMapperProvider.class, method = "batchDeleteByAgreementFreightIds")
    int batchDeleteByAgreementFreightIds(String agreementFreightIds);

}
